<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/8/12 15:34
 * Modified Time: 2019/8/12 15:34
 * Version: 2.0
 */

namespace App\Modules\Inventory\Controllers;


use App\Lib\Facades\FLAuth;
use App\Lib\Utilities;
use App\Modules\Inventory\Services\GoodsCartService;
use Flower\Core\Http\BaseController;
use Illuminate\Http\Request;

class GoodsCartController extends BaseController
{
    protected $goodsCartService;

    public function __construct(GoodsCartService $goodsCartService)
    {
        $this->goodsCartService = $goodsCartService;
    }

    public function getRules()
    {
        return [
            'addGoodsCart' => [
                'store_id' => [
                    'title' => '店铺id',
                    'rule' => 'required',
                    'message' => []
                ],
                'goods_id' => [
                    'title' => '商品id',
                    'rule' => 'required',
                    'message' => []
                ],
                'num' => [
                    'title' => '数量',
                    'rule' => 'required',
                    'message' => []
                ],

            ],
            'getGoodsCart' => [
                'store_id' => [
                    'title' => '店铺id',
                    'rule' => 'required',
                    'message' => []
                ],
            ],
            'delGoodsCart' => [
                'goods_cart_id' => [
                    'title' => '购物车id',
                    'rule' => 'required',
                    'message' => []
                ],
            ],
        ];
    }

    /**
     * 添加购物车
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function addGoodsCart(Request $request)
    {
        $user_id = FLAuth::id();
        $store_id = $request->input('store_id');
        $goods_id = $request->input('goods_id');
        $num = $request->input('num');

        $this->goodsCartService->addGoodsCart($user_id, $store_id, $goods_id, $num);

        return $this->success('操作成功');

    }

    /**
     * 获取购物车
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function getGoodsCart(Request $request)
    {
        $user_id = FLAuth::id();
        $store_id = $request->input('store_id');
        $pageSize = $request->input('pageSize');

        $data = $this->goodsCartService->getGoodsCart($user_id, $store_id, $pageSize);

        return $this->display($data, '查询成功');
    }

    /**
     * 删除购物车
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function delGoodsCart(Request $request)
    {
        $goods_cart_id = $request->input('goods_cart_id');
        $goods_cart_id = explode(',', $goods_cart_id);

        $this->goodsCartService->delGoodsCart($goods_cart_id);

        return $this->success('操作成功');
    }


}